﻿using System;
using System.Collections.Generic;
using System.Text;

namespace GenerationCode.CSClasses
{
    class TableObject
    {


        private Utils.Table table;
        private string projectName;
        private string coder;
        private string email;
        private string objectClass;

        public TableObject(Utils.Table tb, string projectName, string coder, string email)
        {
            this.table = tb;
            this.projectName = projectName;
            this.coder = coder;
            this.email = email;
        }

        private string SetHeader()
        {
            string header = string.Format(@"/*
            Author : {0}
            Email  : {1}
            Date   : {2} 
           
            */

           
            using System;
            using System.Collections.Generic;
            using System.Text;    
        
                    
            namespace {3}{4}
                  /// <summary>
                    /// This is {5} object, it is object of {3} table.
                /// </summary>
                public class {5} {4}",
                this.coder, this.email, DateTime.Now.ToShortDateString(),Utils.Utility.BusinessObjectNamespace, "{",this.table.Name);
            return header;
        }

        private string SetProperties()
        {
            string properties = string.Format(@"
		#region properties"
        );
            foreach (Utils.Column col in this.table.Columns)
            {
                properties += string.Format(@"
                /// <summary>
            /// Define {1} Property
            /// </summary>
		public {0} {1} {3}
			get {3} return this.{2}; {4}
			set {3} this.{2} = value; {4}
		{4}",
                    col.DataType, Utils.Utility.ConvertToPropertyName(col.Name), col.Name, "{", "}");
            }
            properties += @"public Paging ObjectPaging
                            {
                                get{return this.objPaging;}
                                set{this.objPaging=value;}
                            }
            ";
            properties += @"public SortOrder ObjectSortOrder
                            {
                                get{return this.objSortOrder;}
                                set{this.objSortOrder=value;}
                            }
            ";
            properties += @"public string Condition
                            {
                                get{return this.condition;}
                                set{this.condition=value;}
                            }
            ";
            properties += @"public int ToltalRow
                            {
                                get{return this.toltalRow;}
                                set{this.toltalRow=value;}
                            }
            ";
            if (this.table.IsMultilanguage)
            {
                properties += @"
              /// <summary>
            /// Define " + table.Name + Utils.Utility.MultilanguageTableName + Utils.Utility.MultilanguageTableNameList  +  @" Property
            /// </summary>
            public List<" + table.Name + Utils.Utility.MultilanguageTableName + "> " + table.Name + Utils.Utility.MultilanguageTableName + Utils.Utility.MultilanguageTableNameList  + @"
            {
                get { return this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @";}
			    set { this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @"  = value;}
            }
		#endregion properties";
            }
            else
            {
                properties += @"
              #endregion properties";
            }
            return properties;
        }

        private string SetFields()
        {
            string fields = @"
		#region fields";
            //set fields
            foreach (Utils.Column col in this.table.Columns)
            {
                fields += string.Format(@"
		private {0} {1}; ", col.DataType, col.Name);
            }
            if(table.IsMultilanguage)
                fields += "\r\n private List<" + table.Name + Utils.Utility.MultilanguageTableName+  "> lst" + table.Name + Utils.Utility.MultilanguageTableName +  ";\r\n";
            fields += "\r\n private Paging objPaging;\r\n";
            fields += "\r\n private SortOrder objSortOrder;\r\n";
            fields += "\r\n private string condition;\r\n";
            fields += "\r\n private int toltalRow;\r\n";
            

           // fields += "\r\n private static ILog logger = null;";
            fields += string.Format(@"
		
		#endregion fields");


            return fields;
        }

        private string SetConstructor()
        {
            
            string constructor = @"
		
		#region constructor";
            //first constructor
            constructor += string.Format(@"
            /// <summary>
            /// Define default constructor for {0} Object
            /// </summary>
		public {0} (){1}", this.table.Name, "{");
            foreach (Utils.Column col in this.table.Columns)
            {
                constructor += string.Format(@"
			this.{0} = {1};", col.Name, Utils.Utility.InitializeDataType(col.DataType));
            }
            constructor += "this.objPaging =new Paging();\r\n";
            constructor += "this.objSortOrder =new SortOrder();\r\n";
            constructor += "this.condition =string.Empty;\r\n";
            constructor += "this.toltalRow=0;\r\n";


            if(this.table.IsMultilanguage)
                constructor += @"
                this.lst" + table.Name + Utils.Utility.MultilanguageTableName + @"=null;
		        }";
            else
                constructor += @"                
		        }";
            
      
            constructor += @"
		#endregion constructor";

            return constructor;
        }

        private string SetFooter()
        {
            string footer = @"
	}
}";
            return footer;
        }

        public string ObjectClass
        {
            get
            {
                this.objectClass = this.SetHeader() + this.SetFields() + this.SetConstructor() + this.SetProperties() + this.SetFooter();
                return this.objectClass;
            }
        }
    }
}
